//
//  ViewController.m
//  BubbleTea
//
//  Created by Mac on 15/11/19.
//  Copyright © 2015年 riverlj. All rights reserved.
//

#import "ViewController.h"
#import <CoreData/CoreData.h>
#import "Venue.h"
#import "PriceInfo.h"
#import "FilterViewController.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSFetchRequest *fetchRequest;
    NSArray *venues;
    NSAsynchronousFetchRequest *asyncFetchRequest;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSManagedObjectModel *model = [self.managedContext persistentStoreCoordinator].managedObjectModel;
//    fetchRequest = [model fetchRequestTemplateForName:@"FetchRequest"];
//
    NSBatchUpdateRequest *batchUpdate = [[NSBatchUpdateRequest alloc]initWithEntityName:@"Venue"];
    batchUpdate.propertiesToUpdate = @{@"favorite":[NSNumber numberWithBool:true]};

    batchUpdate.affectedStores = self.managedContext.persistentStoreCoordinator.persistentStores;
    
    [batchUpdate setResultType:NSUpdatedObjectsCountResultType];
    
     NSBatchUpdateResult *batchResult = (NSBatchUpdateResult*)[self.managedContext executeRequest:batchUpdate error:nil];
    NSLog(@"Records updated %@", batchResult.result);
    
    
    
    venues = [[NSArray alloc]init];
    
    fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Venue"];
    __weak ViewController *vc = self;
    asyncFetchRequest = [[NSAsynchronousFetchRequest alloc]initWithFetchRequest:fetchRequest completionBlock:^(NSAsynchronousFetchResult * _Nonnull result) {
        venues = result.finalResult;
        [self.tableView reloadData];
    }];
    [self.managedContext executeRequest:asyncFetchRequest error:nil];
    
//    [self fetchAndReadLoad];
}
- (void)fetchAndReadLoad {
    NSError *error = nil;
    venues = [self.managedContext executeFetchRequest:fetchRequest error:&error];
    if (error) {
        NSLog(@"%@",error);
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)unwindSegueToRedViewController:(UIStoryboardSegue *)segue {
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return venues.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VenueCell"];
    Venue *venue = venues[indexPath.row];
    cell.textLabel.text = venue.name;
    PriceInfo *priceInfo = venue.priceInfo;
    cell.detailTextLabel.text = priceInfo.priceCategory;
    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString: @"filterViewControllerSegueIdentifier"]) {
        UINavigationController *navCtl = (UINavigationController *)segue.destinationViewController;
        FilterViewController *filerCtl = (FilterViewController*)navCtl.topViewController;
        filerCtl.managedObjectContext = self.managedContext;
        filerCtl.delegate = self;
    }
}

-(void)filterViewController:(id)filter didSelectPredicate:(NSPredicate *)predicate SortDescriptor:(NSSortDescriptor *)sortDescriptor{
    
    fetchRequest.predicate = nil;
    fetchRequest.sortDescriptors = nil;
    
    NSPredicate *fetchPredicate = predicate;
    if(fetchPredicate) {
        fetchRequest.predicate = fetchPredicate;
    }
    NSSortDescriptor *sr = sortDescriptor;
    if (sr){
        fetchRequest.sortDescriptors = @[sr];
    }
    [self fetchAndReadLoad];
}


@end
